package com.yun.baymax.activity.account;

import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.yun.baymax.R;
import com.yun.baymax.activity.BaseActivity;
import com.yun.lib.utils.L;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 绑定邮箱,会发送邮件至您所填写的邮箱,请确保邮箱的正确性。
 * <p>
 * 邮件发出后,请登录邮箱进行验证。验证成功后,刷新可获最新验证状态。
 */
public class BindEmailActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.bind_email)
    TextInputLayout bindEmail;
    @BindView(R.id.bind_confirm)
    Button bindConfirm;
    @BindView(R.id.progress_bar_area)
    RelativeLayout progressBarArea;
    @BindView(R.id.edit_text)
    TextInputEditText editText;
    String email;
    private BmobUser currentUser;

    @Override
    protected int setLayoutResourceId() {
        return R.layout.activity_bind_email;
    }

    @Override
    protected void initVariables() {
        ButterKnife.bind(this);
        email = getIntent().getStringExtra("title");
        currentUser = BmobUser.getCurrentUser();
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        bindEmail.getEditText().setText(email);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                email = s.toString();
                if (!TextUtils.isEmpty(email) && email.contains("@") && !email.startsWith("@") && !email.endsWith("@")) {
                    bindConfirm.setSelected(true);
                    bindConfirm.setClickable(true);
                } else {
                    bindConfirm.setSelected(false);
                    bindConfirm.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void loadData() {

    }

    @OnClick(R.id.bind_confirm)
    public void onClick() {

        final String email = bindEmail.getEditText().getText().toString().trim();

        if (TextUtils.isEmpty(email) || !email.contains("@") || email.startsWith("@") || email.endsWith("@")) {
            showToast("请输入正确的邮箱");
            return;
        }

        currentUser.setEmail(email);
        progressBarArea.setVisibility(View.VISIBLE);
        currentUser.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                progressBarArea.setVisibility(View.GONE);
                if (e == null) {
                    currentUser.requestEmailVerify(email, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            L.i(e.getMessage());
                            if (e != null) {
                                showToast(e.getMessage());
                            } else {
                                showToast("请求验证邮件成功，请到" + email + "邮箱中进行激活。");
                            }
                        }
                    });
                } else {
                    String message = "该邮箱无效";
                    if (e.getMessage().contains("already taken"))
                        message = "该邮箱已存在";
                    showToast(message);
                }
            }
        });
    }
}
